module AboutYou
  module SDK
    module Model
      ###
      # This class represents a Facet model
      ###
      class Facet
        # Group Id for Brands
        FACET_BRAND = 0
        # Group Id for Color
        FACET_COLOR = 1
        # Group Id for Size
        FACET_SIZE = 2
        # Group Id for Gender/Age
        FACET_GENDERAGE = 3
        # Group Id for Cupsize
        FACET_CUPSIZE = 4
        # Group Id for Length
        FACET_LENGTH = 5
        # Group Id for Dimensions
        FACET_DIMENSION3 = 6
        # Group Id for Size-Codes
        FACET_SIZE_CODE = 206
        # Group Id for Size_Run
        FACET_SIZE_RUN = 172
        # Group Id for Clothing_Unisex_Int
        FACET_CLOTHING_UNISEX_INT = 173
        # Group Id for Clothing_Unisex_Inch
        FACET_CLOTHING_UNISEX_INCH = 174
        # Group Id for Clothing_Unisex_Eur
        FACET_SHOES_UNISEX_EUR = 194
        # Group Id for Clothing_Women_DE
        FACET_CLOTHING_WOMEN_DE = 175
        # Group Id for Clothing_Unisex_Onesize
        FACET_CLOTHING_UNISEX_ONESIZE = 204
        # Group Id for Shoes_Unisex_Adidas_Eur
        FACET_SHOES_UNISEX_ADIDAS_EUR = 195
        # Group Id for Clothing_Women_Belts_Cm
        FACET_CLOTHING_WOMEN_BELTS_CM = 181
        # Group Id for Clothing_Women_Inch
        FACET_CLOTHING_WOMEN_INCH = 180
        # Group Id for Clothing_Men_Belts_Cm
        FACET_CLOTHING_MEN_BELTS_CM = 190
        # Group Id for Clothing_Men_Inch
        FACET_CLOTHING_MEN_INCH = 189
        # Group Id for Clothing_Men_De
        FACET_CLOTHING_MEN_DE = 187
        # Group Id for Condition
        FACET_CONDITION = 234
        # Group Id for Quantity_Per_Pack
        FACET_QUANTITY_PER_PACK = 263
        # Group Id for Season Code
        FACET_SEASON_CODE = 289
        # api-call name for requiring all facets
        FACETS_ALL = '_all'
        # api-call-value for requiring unlimited facets
        FACETS_UNLIMITED = -1

        # json_object with which this instance was created
        attr_accessor :json_object
        # id of the facet
        attr_accessor :id
        # name of the facet
        attr_accessor :name
        # value of the facet
        attr_accessor :value
        # id of the group of the facet
        attr_accessor :group_id
        # name of the group of the facet
        attr_accessor :group_name
        # options of the facet
        attr_accessor :options

        ###
        # Constructor for the AboutYou::SDK::Model::Facet class
        #
        # * *Args* :
        #   - +id+ -> the id of the facet
        #   - +name+ -> the name of the facet
        #   - +value+ -> the value of the facet
        #   - +group_id+ -> id of the group of the facet
        #   - +group_name+ -> name of the group of the facet
        #   - +options+ -> options of the facet
        #
        # * *Returns* :
        #   - an instance of AboutYou::SDK::Model::Facet
        ###
        def initialize(id, name, value, group_id, group_name, options = nil)
          self.id         = id
          self.name       = name
          self.value      = value
          self.group_id   = group_id
          self.group_name = group_name
          self.options    = options

          self
        end

        ###
        # This method is used for creating an instance of this class by a json_object.
        #
        # * *Args*    :
        #   - +json_object+ -> the json_object received from the api
        #
        # * *Returns* :
        #   - Instance of AboutYou::SDK::Model::Facet
        ###
        def self.create_from_json(json_object)
          new(
              json_object['facet_id'],
              json_object['name'],
              json_object['value'] ? json_object['value'] : nil,
              json_object['id'],
              json_object['group_name'],
              json_object['options'] ? json_object['options'] : nil
          )
        end

        ###
        # This method is used for creating a unique key for a facet
        #
        # * *Args* :
        #   - +group_id+ -> id of the group of a facet
        #   - +facet_id+ -> id of a facet
        #
        # * *Returns* :
        #   - a String containing a unique key for a facet
        ###
        def unique_key(group_id = nil, facet_id = nil)
          return String(group_id) + ':' + String(facet_id) unless
          group_id.nil? && facet_id.nil?

          String(self.group_id) + ':' + String(id) unless
          self.group_id.nil? && self.facet_id.nil?
        end

        ###
        # Getter for options
        #
        # * *Args* :
        #   - +key+ -> The key for which options should be returned
        #
        # * *Returns* :
        #   - either null if no options are set or a String containing an option
        ###
        def option(key)
          return unless options
          options.each do |option|
            return option['value'] if option['key'] == key
          end
        end
      end
    end
  end
end
